import 'package:xmpp_stone/xmpp_stone.dart';

/// @description 部分XMPP api
/// @author ping
/// @data 2021/9/2
class XmppApi {
  Connection _connection;
  Jid _jid;

  XmppApi() {}

  void init(Connection connection, Jid jid) {
    _connection = connection;
    _jid = jid;
  }

  String get jidString {
    return _jid.fullJid;
  }

  /*******************群相关************************/

  ///加群
  void presenceGroup(Jid groupJid, {String groupNickNam = 'appUser'}) {
    _connection.write(
        '''
        <presence from="$jidString" to="${groupJid.fullJid}/$groupNickNam" xmlns="jabber:client"><x xmlns="http://jabber.org/protocol/muc"><history/></x></presence>
      '''
    );
  }

  ///退群
  void unavailableGroup(Jid groupJid, {String groupNickNam = 'appUser'}) {
    _connection.write(
        '''
        <presence to="${groupJid.fullJid}/$groupNickNam" type="unavailable" xmlns="jabber:client"><priority>0</priority></presence>
      '''
    );
  }

  ///获取群列表
  void getGroupList() {
    _connection.write(
        '''
        <iq from="$jidString"  to="conference.${_jid.domain}" type="get" xmlns="jabber:client"><query xmlns="http://jabber.org/protocol/disco#items"/></iq> 
      '''
    );
  }

  /*******************注册相关************************/

  ///获取注册所需信息
  void getRegisterNeed() {
    _connection.write(
        '''
          <iq type='get' to= '${_jid.domain}'>
              <query xmlns='jabber:iq:register'/>
          </iq>
        '''
    );
  }


  ///注册(name:Full name全名)
  void register(String username, String password, {String name, String email}) {
    _connection.write(
        '''
        <iq type="set" xmlns="jabber:client">
        <query xmlns="jabber:iq:register">
            <username>$username</username>
            <name>$name</name>
            <email>$email</email>
            <password>$password</password>
        </query>
        </iq>
    '''
    );
  }

  ///注销账号(注销当前登陆账号)
  void unRegister() {
    _connection.write(
        '''
      <iq to='$jidString' type='set'>
        <query xmlns='jabber:iq:register'>
          <remove/>
        </query>
      </iq>
    '''
    );
  }


  /*******************消息相关************************/

  ///根据jid筛选历史聊天记录（set内为设置返回条数pageSize）
  void messageFilterByJid(Jid jid,{int maxSize = 10}) {
    _connection.write(
        '''
      <iq type='set'>
        <query xmlns='urn:xmpp:mam:2'>
          <x xmlns='jabber:x:data' type='submit'>
            <field var='FORM_TYPE' type='hidden'>
              <value>urn:xmpp:mam:2</value>
            </field>
            <field var='with'>
              <value>${jid.fullJid}</value>
            </field>
          </x>
          <set xmlns='http://jabber.org/protocol/rsm'>
            <max>$maxSize</max>
          </set>
        </query>
      </iq>
    '''
    );
  }

  ///获取最近的聊天记录
  void messageFilterByTime({String time}) {
    _connection.write(
        '''
      <iq type='set'>
        <query xmlns='urn:xmpp:mam:2'>
            <value>urn:xmpp:mam:2</value>
            <x xmlns='jabber:x:data' type='submit'>
            <field var='FORM_TYPE' type='hidden'>
              <value>urn:xmpp:mam:2</value>
            </field>
            <field var='start'>
              <value>${time ?? DateTime.now().toString()}</value>
            </field>
          </x>
          <set xmlns='http://jabber.org/protocol/rsm'>
            <max>10</max>
            <before/>
          </set>
        </query>
      </iq>
    '''
    );
  }

  /**************************权限相关******************************/

  void getPermission() {
    _connection.write(
        '''
      <iq from='${_jid.fullJid}' id='some_expression' to='${_jid.domain}' type='get'>
        <query xmlns='http://jabber.org/protocol/disco#items'/>
      </iq>
    '''
    );
  }
}
